package pantomime.gestures;
import pantomime.gestures.*;
import pantomime.hardware.*;
import processing.xml.*;
import processing.core.*;

import java.util.ArrayList;

/**
 * @author PPatino
 * This class is a utility class used for loading gestures from an XML file.
 *
 */
public class GestureLoader {

	public static ArrayList<Gesture> getGesturesFromXML(PApplet parent, String sFileName){
		
		ArrayList<Gesture> alXMLGestures = new ArrayList<Gesture>();
		StaticGesture oStaticGest;
		
		XMLElement xml;
		xml = new XMLElement(parent, sFileName);

		int nGestureTypes = xml.getChildCount();
		
		for(int i = 0; i < nGestureTypes; i++){
			XMLElement xGestureType = xml.getChild(i);
			int nGestures = xGestureType.getChildCount();
			
			for(int j = 0; j < nGestures; j++){
				oStaticGest = new StaticGesture();
				XMLElement xGesture = xGestureType.getChild(j);
				//name, id
				
				oStaticGest.setName(xGesture.getAttribute("name"));
				oStaticGest.setID(Integer.parseInt(xGesture.getAttribute("id")));
				
				
				int nSensors = xGesture.getChildCount();
				
				for(int k = 0; k < nSensors; k++){
					XMLElement xSensorType = xGesture.getChild(k);
					int nSensorCount = xSensorType.getChildCount();
					
					for(int l = 0; l < nSensorCount; l++){
						
						XMLElement xSensorValue = xSensorType.getChild(l);
						String sSensorName = xSensorValue.getName();
						float nReading = Float.parseFloat(xSensorValue.getContent()); 
						if(xSensorType.getName().equalsIgnoreCase("IMU")){
							oStaticGest.oIMU.setIMUValueByName(IMU.Sensor.fromString(sSensorName),  nReading);
						}
						else if(xSensorType.getName().equalsIgnoreCase("Flex")){
							oStaticGest.oFlexSensor.setFlexValueByName(pantomime.hardware.FlexSensors.Sensor.fromString(sSensorName),(int) nReading);
						}					
					}
				}
				alXMLGestures.add(oStaticGest);
			}
		}
		return alXMLGestures;
	}
}